#include "MainMenu.h"
#include "Setting.h"
using namespace cocos2d;

CCScene* MainMenu::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::node();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        MainMenu *layer = MainMenu::node();
        CC_BREAK_IF(! layer);
		
        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MainMenu::init()
{
	
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());
		//set the background
		CCSize size = CCDirector::sharedDirector()->getWinSize();
        CCSprite* pSprite = CCSprite::spriteWithFile("res/menu.png");
        CC_BREAK_IF(! pSprite);
        pSprite->setPosition(ccp(size.width/2, size.height/2));
        this->addChild(pSprite, 0);
		//build menu
		//play
        CCMenuItemImage *pPlayItem = CCMenuItemImage::itemFromNormalImage(
            "res/menu_button_play2.png",
            "res/menu_button_play.png",
            this,
            menu_selector(MainMenu::menuPlayCallback));
        CC_BREAK_IF(! pPlayItem);
		pPlayItem->setPosition(ccp(size.width/2,size.height/4*3));
		//setting
		CCMenuItemImage *pSettingItem = CCMenuItemImage::itemFromNormalImage(
            "res/menu_button_setting2.png",
            "res/menu_button_setting.png",
            this,
            menu_selector(MainMenu::menuSettingCallback));
        CC_BREAK_IF(! pSettingItem);
		pSettingItem->setPosition(ccp(size.width/2,size.height/2));
		//about
		CCMenuItemImage *pAboutItem = CCMenuItemImage::itemFromNormalImage(
            "res/menu_button_about2.png",
            "res/menu_button_about.png",
            this,
            menu_selector(MainMenu::menuAboutCallback));
        CC_BREAK_IF(! pAboutItem);
		pAboutItem->setPosition(ccp(size.width/2,size.height/4));
		//
		CCMenu* pMenu = CCMenu::menuWithItems(pPlayItem, NULL);
		pMenu->addChild(pSettingItem,0);
		pMenu->addChild(pAboutItem,0);

        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);
        this->addChild(pMenu, 1);

        bRet = true;
    } while (0);

    return bRet;
}

void MainMenu::menuPlayCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();
}
void MainMenu::menuSettingCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->replaceScene(CCTransitionZoomFlipY::transitionWithDuration(1.0,Setting::scene(), kOrientationDownOver));
}
void MainMenu::menuAboutCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();
}